package com.szrt.filter;

import java.io.IOException;
import java.nio.file.attribute.UserPrincipalNotFoundException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.gson.Gson;

public class PermissionFilter implements Filter {

	@Override
	public void init(FilterConfig filterConfig) throws ServletException {

	}

	@Override
	public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain)
			throws IOException, ServletException {
		
		
		System.out.println("<<<<<<<<<<第2个过滤器....");
		HttpServletRequest request = (HttpServletRequest) req;
		HttpServletResponse response = (HttpServletResponse) res;
		
		String username= request.getParameter("username")==null?"":request.getParameter("username");
		
		if(username.startsWith("adm")){
			
			//放过
			chain.doFilter(request, response);
		}else{
			//拦截
			//response.sendRedirect("permission.jsp");
			
			Map<String, Object> map = new HashMap<String, Object>();
			/*if("老黄".equals(request.getParameter("username"))){
				map.put("code", "200");
			}else{
				map.put("code", "404");
			}*/
			Gson json = new Gson();
			map.put("code", "405");
			response.getWriter().write(json.toJson(map));
		}

	}

	@Override
	public void destroy() {

	}

}
